package com.yueqianshop.seckill.controller;

import com.yueqianshop.common.utils.R;
import com.yueqianshop.seckill.sevice.SeckillService;
import com.yueqianshop.seckill.vo.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
@Controller
public class SeckillController {

    @Autowired
    SeckillService seckillService;

    @GetMapping("/currentSeckillSkus")
    @ResponseBody
    public R getCurrentSeckillSkus(){
        List<SeckillSkuRedisTo> data = seckillService.getCurrentSeckillSku();
        System.out.println(data);
        return R.ok().setData(data);
    }


    @GetMapping("/sku/seckill/{skuId}")
    @ResponseBody
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId){
        SeckillSkuRedisTo seckillSkuInfo = seckillService.getSkuSeckillInfo(skuId);
        return R.ok().setData(seckillSkuInfo);
    }

    @GetMapping("/seckill")
    public String secKill(@RequestParam("killId") String killId,
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num,
                          Model model) {
        String orderSn = seckillService.seckill(killId,key,num);
        model.addAttribute("orderSn",orderSn);
        return "success";
    }

}
